package com.customizeview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
    private static final String TAG = "MyView";
    private Paint mPaint = new Paint();
    private float value;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 400f);
        valueAnimator.setDuration(3000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                value = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.start();
    }

    private void initPaint() {
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(1);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setTextSize(40);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制直线
        canvas.drawLine(0, 100, 100, 100, mPaint);
        //绘制矩形
        canvas.drawRect(150, 50, 300, 200, mPaint);
        //绘制圆角矩形
        RectF rectF = new RectF(350, 50, 500, 200);
        canvas.drawRoundRect(rectF, 100, 400, mPaint);
        //绘制圆
        canvas.drawCircle(600, 120, 80, mPaint);
        //绘制圆弧
        canvas.drawArc(0, 250, 300, 500, 225, 90, true, mPaint);
        //绘制椭圆
        RectF rectF1 = new RectF(300, 220, 500, 420);
        canvas.drawOval(rectF1, mPaint);
        //绘制折线
        Path path = new Path();
        path.lineTo(0, 530);
        path.lineTo(50, 500);
        path.lineTo(100, 530);
        path.lineTo(150, 500);
        canvas.drawPath(path, mPaint);
        //绘制文本
        canvas.drawText("我想睡觉", 200, 550, mPaint);
        //绘制图片
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.panda);
        canvas.drawBitmap(bitmap, 450, 450, mPaint);
        Matrix reverseMatrix = new Matrix();

        //反转四格图
        reverseMatrix.setScale(1f, 1f);
        reverseMatrix.postTranslate(0, 700);
        canvas.drawBitmap(bitmap, reverseMatrix, mPaint);

        reverseMatrix.setScale(-1f, 1f);
        reverseMatrix.postTranslate(300, 700);
        canvas.drawBitmap(bitmap, reverseMatrix, mPaint);

        reverseMatrix.setScale(1f, -1f);
        reverseMatrix.postTranslate(0, 1000);
        canvas.drawBitmap(bitmap, reverseMatrix, mPaint);

        reverseMatrix.setScale(-1f, -1f);
        reverseMatrix.postTranslate(300, 1000);
        canvas.drawBitmap(bitmap, reverseMatrix, mPaint);

        //旋转四格图
        //set会清空matrix的状态，所以要用post
        Matrix rotateMatrix = new Matrix();
        rotateMatrix.setTranslate(350, 700);
        canvas.drawBitmap(bitmap, rotateMatrix, mPaint);

        rotateMatrix.setRotate(90, 100, 100);
        rotateMatrix.postTranslate(450, 700);
        canvas.drawBitmap(bitmap, rotateMatrix, mPaint);

        rotateMatrix.setRotate(180, 100, 100);
        rotateMatrix.postTranslate(300, 800);
        canvas.drawBitmap(bitmap, rotateMatrix, mPaint);

        rotateMatrix.setRotate(270, 100, 100);
        rotateMatrix.postTranslate(500, 800);
        canvas.drawBitmap(bitmap, rotateMatrix, mPaint);

        //valueAnimator移动
        canvas.drawCircle(value, value, 50, mPaint);
    }
}
